import DataItemModel from "../../../cfw/mvc/DataItemModel";
import { DecorationItemModelEnum } from "../../../public/GameEnumConfig";
import User from "../../db/User";
import DecorationManager from "../DecorationManager";
import DecorationTypeModel from "./DecorationTypeModel";
import { ItemState } from "../../../cfw/tools/Define";
import Player from "../../player/Player";
/**
* 房间装饰品
**/
export default class DecorationItemModel extends DataItemModel {

	static CLASS_NAME: string = 'DecorationItemModel'

	init(id, data) {
		super.init(id, data)
		this.state = User.instance().getDecorationState(this.ID)
	}


	setState(s) {
		User.instance().setDecorationState(this.ID, s)
		super.setState(s)

	}
	// icon
	getIcon() {
		let typeModel = DecorationManager.instance().getType(this.getType());
		return 'texture/' + typeModel.getFolderName() + '/' + this.data[DecorationItemModelEnum.icon]
		// return this.data[DecorationItemModelEnum.icon]
	}
	// 图标缩放
	getScale() {
		return this.data[DecorationItemModelEnum.scale]
	}
	// 使用道具
	getSprite() {
		let res = this.data[DecorationItemModelEnum.sprite]
		let typeModel = DecorationManager.instance().getType(this.getType());
		if (res) {
			return 'texture/' + typeModel.getFolderName() + '/' + res
		} else {
			return 'texture/' + typeModel.getFolderName() + '/' + this.data[DecorationItemModelEnum.icon]
		}
	}

	isOpen() {
		return Player.instance().getLevel() >= this.getOpenLevel()
	}
	getOpenLevel() {
		let num = this.data[DecorationItemModelEnum.adge]
		return num ? num : 0
	}
	needBuy() {
		let cost: number[] = this.getCostItem()
		return cost.length > 0 && this.getState() != ItemState.GOT
	}

	getBack() {
		let res = this.data[DecorationItemModelEnum.back]
		if (res) {
			let typeModel = DecorationManager.instance().getType(this.getType());
			return 'texture/' + typeModel.getFolderName() + '/' + res;
		}
		return null;
	}
	// 消费道具
	getCostItem() {
		return this.data[DecorationItemModelEnum.costItem]
	}
	// 类型
	getType() {
		return this.data[DecorationItemModelEnum.type]
	}
	// 属于哪一栏
	getColumnID() {
		return this.data[DecorationItemModelEnum.columnID]
	}
	// 偏移量
	getOffset() {
		return this.data[DecorationItemModelEnum.offset]
	}

	use() {
		User.instance().setDecorationTypeData(this.getType(), this.getID())
	}

	unUse() {
		User.instance().setDecorationTypeData(this.getType(), 0)
	}
	canDel() {
		let typeModel: DecorationTypeModel = DecorationManager.instance().getType(this.getType());
		return typeModel.getCanDel() == 1;
	}
}